package web.servlet;

import base.BaseServlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import org.apache.commons.beanutils.BeanUtils;
import pojo.Course;
import pojo.Course_Lesson;
import pojo.Course_Section;
import service.CourseContentService;
import service.imp.CourseContentServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    //展示课程对应的章节与课时信息
    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response){
        try {
            String course_id = request.getParameter("course_id");

            CourseContentService contentService = new CourseContentServiceImpl();

            List<Course_Section> sections = contentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));

            String result = JSON.toJSONString(sections);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //课程信息回显
    public void findCourseById(HttpServletRequest request, HttpServletResponse response){
        try {
            String course_id = request.getParameter("course_id");

            CourseContentService contentService = new CourseContentServiceImpl();
            Course course = contentService.findCourseByCourseId(Integer.parseInt(course_id));

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");
            String result = JSON.toJSONString(course, filter);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //保存修改章节信息
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String,Object> map = (Map) request.getAttribute("map");

            Course_Section section = new Course_Section();

            BeanUtils.populate(section,map);

            CourseContentService contentService = new CourseContentServiceImpl();
            String result = null;
            if (section.getId() ==  0){
                result = contentService.saveSection(section);
            }else {
                result=contentService.updateSection(section);
            }

            response.getWriter().print(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //修改章节状态
    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response){
        try {
            String id = request.getParameter("id");
            String status = request.getParameter("status");

            CourseContentService contentService =new CourseContentServiceImpl();
            String result = contentService.updateSectionStatus(Integer.parseInt(id), Integer.parseInt(status));

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //保存课时信息
    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String,Object> map = (Map) request.getAttribute("map");

            Course_Lesson lesson = new Course_Lesson();
            BeanUtils.populate(lesson,map);

            CourseContentService contentService = new CourseContentServiceImpl();
            String result = null;
            if (lesson.getId() ==  0){
                result = contentService.saveLesson(lesson);
            }else {
                result = contentService.updateLesson(lesson);
            }
            response.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
